package com.hs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.entity.dto.Order;
import com.hs.entity.dto.Notice;
import com.hs.entity.dto.Order;
import com.hs.mapper.OrderMapper;
import com.hs.mapper.NoticeMapper;
import com.hs.mapper.OrderMapper;
import com.hs.service.NoticeService;
import com.hs.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Resource
    private OrderMapper orderMapper;

    @Override
    public PageInfo<Order> selectOrderList(Integer currentNum, String keywords) {
        PageHelper.startPage(currentNum, 3);

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("o.create_time"); // 添加排序

        if (keywords != null && !keywords.isEmpty()) {
            queryWrapper.like("o.start_location", keywords)
                    .or()
                    .like("o.end_location", keywords);
        }

        List<Order> orderList = orderMapper.selectListWithTeamAndUser(queryWrapper);
        return new PageInfo<>(orderList);
    }

    @Override
    public PageInfo<Order> selectOrderListByUser(Integer currentNum, String keywords, Integer userId) {
        PageHelper.startPage(currentNum, 3);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId); // 添加用户ID条件

        if (StringUtils.isNotBlank(keywords)) {
            wrapper.like("o.id", keywords);
        }

        wrapper.orderByDesc("create_time");
        List<Order> orders = orderMapper.selectListWithTeamAndUser(wrapper);
        return new PageInfo<>(orders);
    }

    @Override
    public PageInfo<Order> selectOrderListByTeamId(Integer currentNum, String keywords, Integer teamId) {
        PageHelper.startPage(currentNum, 3);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("o.team_id", teamId);

        if (StringUtils.isNotBlank(keywords)) {
            wrapper.like("start_location", keywords).or().like("end_location", keywords);
        }

        wrapper.orderByDesc("create_time");
        List<Order> orders = orderMapper.selectListWithTeamAndUser(wrapper);
        return new PageInfo<>(orders);
    }

    @Override
    public void updateOrderStatus(Integer id, Integer status) {
        Order order = orderMapper.selectOrderById(id);
        if (order != null) {
            // 更新状态
            order.setStatus(status);
            orderMapper.updateById(order);
        } else {
            throw new RuntimeException("Order not found");
        }
    }

    @Override
    public List<Order> selectAllOrderListByTeamId(Integer teamId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("o.team_id", teamId);

        wrapper.orderByDesc("create_time");
        return orderMapper.selectListWithTeamAndUser(wrapper);
    }

    @Override
    public List<Order> selectAllOrderList() {
        return orderMapper.selectListWithTeamAndUser(null);
    }


}
